<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>合作供应商占比</title>
    <!-- 引入 ECharts 库 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.0/echarts.min.js"></script>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f0f0; /* 背景颜色 */
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        #pie {
            width: 800px;
            height: 600px;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            background-color: #fff; /* 图表背景颜色 */
        }
    </style>
</head>
<body>

<div id="pie"></div>

<script>
    // 初始化 ECharts 实例
    var chartDom = document.getElementById('pie');
    var myChart = echarts.init(chartDom);

    // 设置图表的配置项和数据
    var option = {
        title: {
            text: '合作供应商占比',
            subtext: 'Data from MySQL',
            left: 'center',
            textStyle: {
                fontSize: 24,
                fontWeight: 'bold', // 加粗标题字体
                color: '#333' // 标题颜色
            }
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            textStyle: {
                fontSize: 14,
                color: '#555' // 图例文字颜色
            }
        },
        series: [
            {
                name: '供应商',
                type: 'pie',
                radius: ['40%', '70%'], // 调整内外半径，使图表更大
                avoidLabelOverlap: false,
                label: {
                    show: true,
                    position: 'outside',
                    formatter: '{b}: {c} ({d}%)',
                    textStyle: {
                        fontSize: 16,
                        color: '#333' // 标签文字颜色
                    }
                },
                labelLine: {
                    show: true,
                    length: 10,
                    length2: 15
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: '18',
                        fontWeight: 'bold'
                    }
                },
                data: []
            }
        ]
    };

    // 使用 fetch 获取数据并设置图表数据
    fetch("/stats/getbycustomer")
        .then(response => response.json())
        .then(res => {
            console.log('Response from backend:', res);
            if (res.code === 1) {
                res.data.forEach(item => {
                    option.series[0].data.push({ name: item.name, value: item.count });
                });
                myChart.setOption(option);
            } else {
                console.error('Error:', res.msg);
            }
        })
        .catch(error => console.error('Error fetching data:', error));
</script>

</body>
</html>
